package com.youdianda.service.impl;

import com.youdianda.constant.MessageConstant;
import com.youdianda.dto.SysMenuFgxDTO;
import com.youdianda.entity.SysFgxMenu;
import com.youdianda.mapper.SysMenuFgxMapper;
import com.youdianda.result.Result;
import com.youdianda.service.SysMenuFgxService;
import com.youdianda.vo.SysMenuFgxAllDataVO;
import com.youdianda.vo.SysMenuFgxVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuFgxServiceImpl implements SysMenuFgxService {
    @Autowired
    private SysMenuFgxMapper sysMenuMapper;

    //所有菜单
    @Transactional
    @Override
    public Result allData() {

        //系统菜单集合
        List<SysMenuFgxAllDataVO> sysMenuAllDataVOList = new ArrayList<>();
        List<SysFgxMenu> menuList = sysMenuMapper.menuList();
        menuList.forEach(sysMenu -> {
            SysMenuFgxAllDataVO sysMenuAllDataVO = new SysMenuFgxAllDataVO();
            BeanUtils.copyProperties(sysMenu,sysMenuAllDataVO);
            //children
            List<SysFgxMenu> sysMenuList = sysMenuMapper.childrenByPid(sysMenu.getId());
            List<SysMenuFgxVO> sysMenuVOS = new ArrayList<>();

            sysMenuList.forEach(sysMenu1 -> {
                SysMenuFgxVO sysMenuVO = new SysMenuFgxVO();
                BeanUtils.copyProperties(sysMenu1,sysMenuVO);

                List<SysFgxMenu> sysMenuList2 = sysMenuMapper.childrenByPid(sysMenu1.getId());
                //添加子children
                sysMenuVO.setChildren(sysMenuList2);

                //添加children
                sysMenuVOS.add(sysMenuVO);
            });

            //设置子集合
            sysMenuAllDataVO.setChildren(sysMenuVOS);
            sysMenuAllDataVOList.add(sysMenuAllDataVO);
        });

        return Result.success(sysMenuAllDataVOList);
    }

    //菜单详情
    @Override
    public Result rowInfo(Integer id) {
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        SysFgxMenu sysMenu = sysMenuMapper.rowInfoById(id);
        return Result.success(sysMenu);
    }

    //菜单修改
    @Override
    public Result update(SysFgxMenu sysMenu) {
        if (sysMenu == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        sysMenu.setUpdate_date(LocalDateTime.now());
        sysMenuMapper.update(sysMenu);
        return Result.success();
    }

    //菜单添加
    @Override
    public Result add(SysMenuFgxDTO dto) {
        if (dto == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        SysFgxMenu sysMenu = new SysFgxMenu();
        BeanUtils.copyProperties(dto,sysMenu);
        sysMenu.setCreate_date(LocalDateTime.now());
        sysMenu.setUpdate_date(LocalDateTime.now());
        sysMenu.setDel_flag(0L);
        sysMenuMapper.add(sysMenu);

        return Result.success();
    }

    //删除(逻辑删除)
    @Override
    public Result del(Integer id) {
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        SysFgxMenu sysMenu = new SysFgxMenu();
        sysMenu.setId(Long.valueOf(id));
        sysMenu.setUpdate_date(LocalDateTime.now());
        sysMenu.setDel_flag(1L);
        sysMenuMapper.update(sysMenu);
        return Result.success();
    }


}
